const mixin = {//通用部分
    computed: {
        Type() {
            return function (row) {
                if (row.Refund_Type=='1') {
                    return ["已退款", "info"]
                } else {
                    let str = row.Row_Status;
                    switch (str.charAt(1)) {
                        case "0":
                            return ["待付款", ""];
                        case "1":
                            return ["待付款", ""];
                        case "2":
                            return ["待付款", ""];
                        case "3":
                            return ["待发货", "success"];
                        case "4":
                            return ["备货中", "warning"];
                        case "5":
                            return ["已取货", "info"];
                        case "6":
                            return ["已取消", "danger"];
                        case "7":
                            return ["已取消", "danger"];
                        default:
                            return ""
                    }
                }
            }
        }
    }
}
Vue.component('yl-button', {
    template: `
    <span>
        <el-button :type="type" @click="Export" :loading="loading">
            <slot></slot>
        </el-button>
        <el-table id="ExportData" :data="data" style="display: none; width: 100%;margin-bottom: 20px;">
        <el-table-column align="center" type="index" label="序号" width="50"></el-table-column>
        <el-table-column align="center" prop="Sales_Order_SerialNumber" label="订单编号"></el-table-column>
        <el-table-column align="center" prop="Sales_Amount" label="订单金额"></el-table-column>
        <el-table-column align="center" prop="Reality_Amount" label="应付金额"></el-table-column>
        <el-table-column align="center" prop="Sales_Point" label="订单积分"></el-table-column>
        <el-table-column align="center" prop="Member_Name" label="用户姓名"></el-table-column>
        <el-table-column align="center" prop="Sales_Contacts" label="收件人"></el-table-column>
        <el-table-column align="center" prop="Sales_Phone" label="联系方式"></el-table-column>
        <el-table-column align="center" prop="Sales_Address" label="收货地址"></el-table-column>
        <el-table-column align="center" prop="Sales_Date" label="下单时间"></el-table-column>
        <el-table-column align="center" prop="Sales_Message" show-overflow-tooltip label="用户备注"></el-table-column>
        <el-table-column align="center" prop="Row_Status" label="订单状态">
            <template v-slot="{row}">
                {{Type(row)[0]}}
            </template>
        </el-table-column>
        </el-table>
    </span>`,
    props: ['loading', 'data', 'type'],
    methods: {
        Export() {
            this.$emit('click')
        }
    },
    mixins: [mixin],
})
new Vue({
    el: '#app',
    data() {
        return {
            form: {
                SQLSerialNumber: "P_business_sales_order",
                PaginationNumber: 1,//页数
                RowAmount: 20,//每页数据条数
                Condition: '',
                ExtensionCondition: '',
                Activity_SerialNumber: '',
                Sales_Order_SerialNumber: '',
                Sales_Date: null,
            },
            Activity: [],
            total: 0,
            tableData: [],	//表格数据
            treeData: [],
            ExportData: [],//导出
            defaultProps: {
                children: 'Children',
                label: 'Supplier_Name'
            },
            loading: false,
            dialogVisible: null,
            Sales_Order: {},//当前选中的订单
            isTree:true,
        }
    },
    mounted() {
        this.getData();
        this.getList(this.form);//前端分页
    },
    mixins: [mixin],
    computed: {
        Amount() {
            return function (row) {
                let Sales_Point = row.Sales_Point == 0 ? '' : row.Sales_Point + '积分';
                let i = row.Sales_Point != 0 && row.Reality_Amount != 0 ? '+' : ''
                let Reality_Amount = row.Reality_Amount == 0 ? '' : row.Reality_Amount + '元'
                return Sales_Point + i + Reality_Amount
            }
        },
    },
    methods: {
        showTree(){
            this.isTree = !this.isTree
        },
        onSerach() {			//查询方法
            this.form.PaginationNumber = 1;
            this.getList(this.form)
        },
        handleSizeChange(val) {
            this.form.PaginationNumber = 1;
            this.form.RowAmount = val;
            this.getList(this.form)
        },
        handleCurrentChange(val) {
            this.form.PaginationNumber = val;
            this.getList(this.form)
        },
        getData() {
            const that = this;
            $.ajax({
                headers: requestHeaders(),
                url: `${BASE_URL2}/generalBusiness/generalQueryByTableName`,
                type: 'POST',
                data: {
                    Data: 'business_activity',
                    Condition: "Row_Status <> '1'",
                },
                success: function (res) {
                    that.Activity = res;
                },
                error: function () {
                    that.$message.error("操作失败，请稍后再试")
                },
                complete: function (XMLHttpRequest) {
                    that.loading = false
                    requestPretreatment(XMLHttpRequest);
                },
                dataType: "json"
            });
            $.ajax({
                headers: requestHeaders(),
                url: `${BASE_URL2}/salesOrder/getSalesOrderTree`,
                type: 'GET',
                success: function (data) {
                    data.forEach(item => {
                        item.Activity_SerialNumberAsAlias = item.Activity_SerialNumber
                        item.Supplier_Name = item.Activity_Name
                    });
                    that.treeData = data;
                },
                complete: function (XMLHttpRequest) {
                    requestPretreatment(XMLHttpRequest);
                },
                dataType: "json"
            });
        },
        getList(form) {//换页方法
            const that = this
            let data = deepClone(form);
            data.Condition = window.btoa(unescape(encodeURIComponent("%" + data.Sales_Order_SerialNumber + "%,%" + data.Sales_Order_SerialNumber + "%,%" + data.Sales_Order_SerialNumber + "%")))
            let sql = "";
            if (data.Sales_Date) {
                sql = sql + "AND business_sales_order.Sales_Date BETWEEN '" + data.Sales_Date[0] + "' AND '" + data.Sales_Date[1] + "'\r\n"
            }
            if (data.Activity_SerialNumber) {
                sql = sql + "AND business_sales_order_detail.Activity_SerialNumber = '" + data.Activity_SerialNumber + "'\r\n"
            }
            if (data.Supplier_SerialNumber) {
                sql = sql + "AND business_sales_order_detail.Supplier_SerialNumber = '" + data.Supplier_SerialNumber + "'\r\n"
            }
            data.ExtensionCondition = window.btoa(unescape(encodeURIComponent(sql)));
            $.ajax({
                headers: requestHeaders(),
                url: `${BASE_URL2}/generalBusiness/generalPaginationQueryBySQLSerialNumber`,
                type: 'POST',
                data,
                success: function (Result) {
                    that.tableData = Result.Rows;
                    that.total = Result.TotalRows;
                },
                complete: function (XMLHttpRequest) {
                    requestPretreatment(XMLHttpRequest);
                },
                dataType: "json"
            });
        },
        Operate(command, row) {
            if (command == 'Show') {
                this.Sales_Order = row
                this.dialogVisible = true;
            }
        },
        handleNode(row, node) {
            this.form.Activity_SerialNumber = row.Activity_SerialNumber;
            this.form.Supplier_SerialNumber = row.Supplier_SerialNumber;
            this.getList(this.form);
        },
        Export(form) {
            const that = this;
            this.loading = true;
            let data = deepClone(form);
            data.Condition = window.btoa(unescape(encodeURIComponent("%" + data.Sales_Order_SerialNumber + "%,%" + data.Sales_Order_SerialNumber + "%,%" + data.Sales_Order_SerialNumber + "%")))
            let sql = "";
            if (data.Sales_Date) {
                sql = sql + "AND business_sales_order.Sales_Date BETWEEN '" + data.Sales_Date[0] + "' AND '" + data.Sales_Date[1] + "'\r\n"
            }
            if (data.Activity_SerialNumber) {
                sql = sql + "AND business_member_points_log.Activity_SerialNumber = '" + data.Activity_SerialNumber + "'\r\n"
            }
            data.ExtensionCondition = window.btoa(unescape(encodeURIComponent(sql)));
            $.ajax({
                headers: requestHeaders(),
                url: `${BASE_URL2}/generalBusiness/generalQueryBySQLSerialNumber`,
                type: 'POST',
                data,
                success: function (Result) {
                    if (Result.length > 0) {
                        that.ExportData = Result;
                        that.$nextTick(() => {
                            let wb = XLSX.utils.table_to_book(document.querySelector("#ExportData"));
                            var wbout = XLSX.write(wb, {
                                bookType: "xlsx",
                                bookSST: true,
                                type: "array"
                            });
                            try {
                                saveAs(
                                    new Blob([wbout], {
                                        type: "application/octet-stream"
                                    }),
                                    "销售订单表" + (+new Date()) + ".xlsx"
                                );
                            } catch (e) {
                                if (typeof console !== "undefined") console.log(e, wbout);
                            }
                            that.$message({
                                message: '已成功导出',
                                type: 'success'
                            })
                            return wbout;
                        })
                    } else {
                        that.$message({
                            message: '暂无可导出的数据',
                            type: 'warning'
                        })
                    }
                },
                complete: function (XMLHttpRequest) {
                    that.loading = false;
                    requestPretreatment(XMLHttpRequest);
                },
                dataType: "json"
            });
        }
    },
})